home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / WASTE Object Handlers 1.2.2.sit / WASTE Object Handlers 1.2.2 / Sample Code / DragTextFile.c next >
Text File  |  1996-07-16  |  5KB  |  181 lines

  1. /*
  2.  
  3. This is an example of how to drag text files to a WASTE instance and
  4. have them imported.  For this example to work, you must be using the 
  5. 'hfs ' type WASTE object supplied with this package.
  6.  
  7. 28 March 1996, compatability updates made for new Universal Headers, WASTE 1.2a5
  8.                 and pre-Copland accessor routines
  9.  
  10. Michael F. Kamprath
  11. kamprath@kagi.com
  12. 19 March 1995
  13. */
  14.  
  15. #ifndef __DRAG__
  16. #include <Drag.h>
  17. #endif
  18.  
  19. #ifndef _WASTE_
  20. #include "WASTE.h"
  21. #endif
  22.  
  23. pascal OSErr    MyWEReceiveHandler(    WindowRef theWindow,
  24.                             Ptr handlerRefCon,
  25.                             DragReference theDrag)
  26. {
  27. ItemReference    theItem;
  28. Size            itemSize;
  29. Ptr                textP;
  30. StScrpHandle    styleH;
  31. WESoupHandle    soupH;
  32. GrafPtr            savePort;
  33. DocPtr            theDoc;
  34. HFSFlavor        **theHFS;
  35. long            insertionOffset, oldNLines, curEOF;
  36. unsigned short    items, index;
  37. short            fRefNum;    
  38. OSErr            iErr;
  39. Point             zeroPoint = {0, 0};
  40. Point             mouse;
  41. LongPt             dropLocation;
  42. char             dropEdge;
  43.  
  44.     if (theWindow != nil)
  45.     {
  46.         GetPort(&savePort);
  47.         SetPortWindowPort(theWindow);
  48.  
  49.         theDoc = (DocPtr)GetWRefCon( theWindow );     // Or recover your document or
  50.                                                     // WASTE handle how ever you see 
  51.                                                     // fit.
  52.         
  53.         oldNLines = WECountLines( theDoc->theWE );
  54.  
  55.         CountDragItems(theDrag, &items);
  56.  
  57.         for (index = 1; index <= items; index++)     // step through each drag item
  58.                                                     // and check for text files
  59.         {
  60.             GetDragItemReferenceNumber(theDrag, index, &theItem);
  61.                 
  62.             if (!(iErr = GetFlavorDataSize(theDrag, theItem, flavorTypeHFS, &itemSize)))
  63.             {
  64.                                                     // Found a text file.  Now import
  65.                                                     // it to the WASTE instance;
  66.                                                     
  67.                 theHFS = (HFSFlavor **)NewHandleClear(sizeof(HFSFlavor));
  68.             
  69.                 if (theHFS)
  70.                 {
  71.                     HLockHi( (Handle)theHFS );
  72.                     GetFlavorData( theDrag, theItem, flavorTypeHFS, *theHFS, &itemSize, 0L);
  73.                     
  74.                     if (( (*theHFS)->fileType == 'TEXT' )||( (*theHFS)->fileType == 'ttro' ))
  75.                     {
  76.                         // Load the text file into memory
  77.                         
  78.                         iErr = FSpOpenDF( &(*theHFS)->fileSpec, fsCurPerm, &fRefNum );
  79.                         iErr = GetEOF(fRefNum,&curEOF);
  80.                         
  81.                         textP = NewPtrClear( curEOF );
  82.     
  83.                         iErr = SetFPos(fRefNum,fsFromStart,0);
  84.                         iErr = FSRead( fRefNum, &curEOF, textP);
  85.                         iErr = FSClose( fRefNum );
  86.     
  87.                         fRefNum = FSpOpenResFile(&(*theHFS)->fileSpec,fsCurPerm);
  88.     
  89.                         if ( fRefNum != -1 )
  90.                         {
  91.                             // has resource fork, so look for style and soup
  92.                             
  93.                             styleH = (StScrpHandle)Get1Resource( 'styl', 128 );
  94.                             iErr = ResError()
  95.                             if (iErr)
  96.                                 styleH = nil;
  97.                             else
  98.                                 DetachResource( (Handle)styleH );
  99.  
  100.                             soupH = (WESoupHandle)Get1Resource( 'SOUP', 128 );
  101.                             iErr = ResError()
  102.                             if (iErr)
  103.                                 soupH = nil;
  104.                             else
  105.                                 DetachResource( (Handle)soupH );
  106.  
  107.         
  108.                             CloseResFile(fRefNum);
  109.                         }
  110.                         else    
  111.                         {
  112.                             // does not have resource fork, so no style or soup.
  113.                             
  114.                             styleH = nil;
  115.                             soupH = nil;
  116.                         }
  117.     
  118.                         // insert the text file into the current position
  119.                         
  120.                         iErr = GetDragMouse(theDrag, &mouse, &zeroPoint);
  121.                         if (iErr != noErr)
  122.                             goto cleanup;
  123.                         GlobalToLocal(&mouse);
  124.  
  125.                         WEPointToLongPoint(mouse, &dropLocation);
  126.                         insertionOffset = WEGetOffset(&dropLocation, &dropEdge, theDoc->theWE);
  127.                         WESetSelection( insertionOffset, insertionOffset, theDoc->theWE );
  128.                              
  129.                         iErr = WEInsert(textP, curEOF, styleH, soupH, theDoc->theWE);
  130.                             
  131.                         WESetSelection( insertionOffset, insertionOffset+curEOF, theDoc->theWE );
  132.                             
  133.                         // dispose of memory
  134. cleanup:                        
  135.                         HUnlock( (Handle)theHFS );
  136.                         DisposeHandle( (Handle)theHFS );
  137.                         DisposePtr( textP );
  138.                         if (styleH) DisposeHandle( (Handle)styleH );
  139.                         if (soupH) DisposeHandle( (Handle)soupH );
  140.                         
  141.                         // update the WASTE instance if need be
  142.                         //
  143.                         // your program will probably do ssomething different
  144.                         
  145.                         if (  WECountLines( theDoc->theWE ) != oldNLines )
  146.                             SetVScroll(theDoc);
  147.                         theDoc->dirty = true;
  148.                         
  149.                         SetPort(savePort);
  150.                         return (iErr);
  151.                     }
  152.                     HUnlock( (Handle)theHFS );
  153.                     DisposeHandle( (Handle)theHFS );
  154.                 }
  155.             }
  156.             
  157.             // If the code got to here, that means there was no
  158.             // text file item in the drag.  Let WASTE handle the drag then.
  159.             
  160.             HideDragHilite(theDrag);  // I found I need to do this for cosmetic purposes sometimes
  161.             
  162.             iErr = WEReceiveDrag(theDrag, theDoc->theWE );
  163.             
  164.             // update the WASTE instance if need be
  165.             //
  166.             // your program will probably do ssomething different
  167.             if (  WECountLines( theDoc->theWE ) != oldNLines )
  168.                 SetVScroll(theDoc);
  169.             theDoc->dirty = true;
  170.         }
  171.         else
  172.             iErr = dragNotAcceptedErr;
  173.  
  174.         SetPort(savePort);
  175.     }
  176.     else
  177.         iErr = noErr;
  178.     
  179.     return(noErr);
  180. }
  181.